package com.starsea.book.ui.newpage.fragment

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.QuickAdapterHelper
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentBookCityBinding
import com.starsea.book.databinding.LayoutHomeHeaderBannerBinding
import com.starsea.book.ui.newpage.adapter.BookCityCenterAdapter
import com.starsea.book.ui.newpage.adapter.BookCityListAdapter
import com.starsea.book.ui.newpage.adapter.BookCityTopAdapter
import com.starsea.book.ui.book.info.BookInfoActivity
import com.starsea.book.ui.book.search.SearchActivity
import com.starsea.book.ui.main.MainFragmentInterface
import com.starsea.book.ui.newpage.activity.BookDepositoryActivity
import com.starsea.book.ui.newpage.activity.BookRanksActivity
import com.starsea.book.ui.newpage.adapter.BookCityTopRecommendAdapter
import com.starsea.book.ui.newpage.adapter.BookRanksTitleAdapter
import com.starsea.book.ui.newpage.adapter.HomeAdapter
import com.starsea.book.ui.newpage.entity.HomeBannerEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.ui.newpage.weidget.banner.Banner
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2023-12-26 09 GMT+8
 * @desc :
 */
class HomeChildFragment() : VMBaseFragment<HomeViewModel>(R.layout.fragment_book_city) {


    lateinit var mAdapter: BookCityListAdapter
    lateinit var mAdapterTop: BookCityTopRecommendAdapter
    lateinit var mAdapterCenter: BookCityCenterAdapter

    //  lateinit var mAdapterHeader: BookCityHeaderAdapter
    private val binding by viewBinding(FragmentBookCityBinding::bind)
    override val viewModel by viewModels<HomeViewModel>()

    var page = 1

    val value: String? get() = arguments?.getString("value")
    val name: String? get() = arguments?.getString("name")

    constructor(name: String, value: String) : this() {
        val bundle = Bundle()
        bundle.putString("name", name)
        bundle.putString("value", value)
        arguments = bundle
    }


    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {

        mAdapter = BookCityListAdapter(requireContext())
        mAdapterTop = BookCityTopRecommendAdapter(requireContext())
        mAdapterCenter = BookCityCenterAdapter(requireContext())
        binding.rvContent.layoutManager = StaggeredGridLayoutManager(2, RecyclerView.VERTICAL)
        binding.rvTop.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
        binding.rvCenter.layoutManager = LinearLayoutManager(requireContext())

        binding.rvTop.adapter = mAdapterTop
        binding.rvCenter.adapter = mAdapterCenter


        mAdapterTop.setOnItemClickListener { adapter, view, position ->
            startActivity(
                BookInfoActivity::class,
                "bookId" to mAdapterTop.items[position].bookId!!
            )
        }
        mAdapterCenter.setOnItemClickListener { adapter, view, position ->
            startActivity(
                BookInfoActivity::class,
                "bookId" to mAdapterCenter.items[position].bookList!!.first().bookId!!
            )
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(
                BookInfoActivity::class,
                "bookId" to mAdapter.items[position].bookId!!
            )
        }

        binding.srlRefresh.setOnRefreshListener {
            page = 1
            viewModel.getBookCityData(value ?: "")
        }

        binding.srlRefresh.setOnLoadMoreListener {
            page++
            viewModel.getBookCityData(value ?: "")
        }

        //排行榜
        binding.rllRanks.setOnClickListener {
            startActivity(BookRanksActivity::class, "value" to value!!)
        }
        //书库
        binding.rllBookDepository.setOnClickListener {
            startActivity<BookDepositoryActivity>()
        }

        val helper = QuickAdapterHelper.Builder(mAdapter)
            .build()

//    helper.addBeforeAdapter(mAdapterHeader)
        binding.rvContent.adapter = helper.adapter

        LogUtils.e(">>>> value = $value")

        viewModel.getFourRecommend(value ?: "")
        viewModel.getIndexRecommend(value ?: "")
        viewModel.getBookCityData(value ?: "")

        viewModel.fourRecommendResutl.observe(this) {
            when (it.status) {
                200 -> {
                    mAdapterCenter.submitList(it.data)
                }

                else -> {

                }
            }
        }

        viewModel.indexRecommendResutl.observe(this) {
            when (it.status) {
                200 -> {
                    mAdapterTop.submitList(it.data)
                }

                else -> {

                }
            }
        }
        viewModel.dataResutl.observe(this) {
            when (it.status) {
                200 -> {
                    if (page == 1) {
                        binding.srlRefresh.finishRefresh()
                        mAdapter.submitList(it.data ?: arrayListOf())
                    } else {
                        binding.srlRefresh.finishLoadMore()
                        mAdapter.addAll(it.data ?: arrayListOf())
                    }

                }

                else -> {

                }
            }
        }
    }


}